package com.woniuxy.store.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.store.app.util.StoreIdUtil;
import com.woniuxy.store.client.StoreWalletClient;
import com.woniuxy.store.client.param.AddBalanceParam;
import com.woniuxy.store.client.param.WithdrawParam;
import com.woniuxy.store.client.result.WalletDto;
import com.woniuxy.store.infra.model.StoreWallet;
import com.woniuxy.store.service.StoreWalletService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import result.Result;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@RequestMapping("/store/withdraw")
public class StoreWalletClientImpl implements StoreWalletClient {

    @Resource
    private StoreWalletService storeWalletService;


    @Override
    public Result<WalletDto> getWallet() {
        StoreWallet storeWallet = storeWalletService.getWallet(StoreIdUtil.getFromToken());
        WalletDto dto = BeanUtil.toBean(storeWallet, WalletDto.class);
        return Result.success(dto);
    }

    @Override
    public Result addBalance(@Valid AddBalanceParam param) {
        storeWalletService.addBalance(StoreIdUtil.getFromToken(),param.getStoreWalletId(),param.getAmount());
        return Result.success();
    }

    @Override
    public Result withdraw(@Valid WithdrawParam param) {
        storeWalletService.withdraw(param.getStoreWalletId(),StoreIdUtil.getFromToken(),param.getAmount());
        return Result.success();
    }

}
